<?php
	class StudentItemController extends Controller{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Finance";
		public $subMenu = "";
		public $title = "";
		
		public function init() {
						
		}
		
		public function actionIndex(){
			//$piutangParentId = AppSetting::getValue("pituang_acc_id");
			$pendapatanParentId = AppSetting::getValue("pendapatan_acc_id");
			$discParentId = AppSetting::getValue("disc_acc_id");
			
			//$piutangParent = MCoa::model()->findByPK($piutangParentId);
			$pendapatanParent = MCoa::model()->findByPK($pendapatanParentId);
			$discParent = MCoa::model()->findByPK($discParentId);
			
			//$arrPiutangCoa =  MCoa::model()->findAll("acc_code like '". $piutangParent->acc_code .".%' and is_active = 1 and is_leaf = 1");
			$arrPendapatanCoa =  MCoa::model()->findAll("acc_code like '". $pendapatanParent->acc_code .".%' and acc_code not like '". $discParent->acc_code .".%' and is_active = 1 and is_leaf = 1");
			$arrDiscCoa =  MCoa::model()->findAll("acc_code like '". $discParent->acc_code .".%' and is_active = 1 and is_leaf = 1");

			
			//$data["arrPiutangCoa"] = $arrPiutangCoa;
			$data["arrPendapatanCoa"] = $arrPendapatanCoa;
			$data["arrDiscCoa"] = $arrDiscCoa;
			
			$this->layout ="//layouts/ly_blank";
			$this->render('index',$data);
		}
		
		public function actionAllList(){
			$arrStudentItem =  MStudentItem::model()->findAll();			
			$arrData = array();
			foreach($arrStudentItem as $studentItem){
				$tmp = array();
				$tmp["id"]= $studentItem->id;
				$tmp["item_desc"]= $studentItem->item_desc;
				$tmp["item_price"]= $studentItem->item_price;
				$tmp["pendapatan_acc_id"]= $studentItem->pendapatan_acc_id;
				$tmp["pendapatan_acc_name"]= $studentItem->pendapatanAcc->name;
				$tmp["disc_acc_id"]= $studentItem->disc_acc_id;
				$tmp["disc_acc_name"]= $studentItem->discAcc->name;				
				$tmp["is_active"]= $studentItem->is_active;
				$tmp["sys_data"]= $studentItem->sys_data;
				$arrData[] = $tmp;
			}
			$this->result["model"] = $arrData;
			$this->returnJSON($this->result);
		}
		
		public function actionEdit(){
			$id = Yii::app()->request->getParam("id","");
			$itemDesc = Yii::app()->request->getParam("item_desc","");
			$itemPrice = Yii::app()->request->getParam("item_price","0");
			$itemPrice = Utils::parseCurrency($itemPrice);
			
			$pendapatan_acc_id = Yii::app()->request->getParam("pendapatan_acc_id","");
			$disc_acc_id = Yii::app()->request->getParam("disc_acc_id","");
			$is_active = Yii::app()->request->getParam("is_active","");
			$op = Yii::app()->request->getParam("op","");			
			if (strtolower($is_active) == ""){
				$is_active =0;
			}else{
				$is_active =1;
			}
			
			try{
				if ($id == ""){
					//new data
					$studentItem = new MStudentItem();
					$studentItem->item_desc = $itemDesc;
					$studentItem->item_price = $itemPrice;
					$studentItem->pendapatan_acc_id = $pendapatan_acc_id;
					$studentItem->disc_acc_id = $disc_acc_id;
					$studentItem->is_active = $is_active;
					if(!$studentItem->save()) {
						$this->result["actionMessage"] = "Gagal menyimpan data transaksi";
						throw new Exception(Yii::t("dialog",json_encode($stundetTransType->getErrors())));
					}
				}else{
					//edit data
					$studentItem = MStudentItem::model()->findByPK($id);
					if ($studentItem == null){
						$this->result["actionMessage"] = "Data system ini tidak di kenal";
						throw new Exception(Yii::t("dialog","data with id $id not found .."));
					}
					
					if($op == "del"){
						if($studentItem->sys_data){
							$this->result["actionMessage"] = "Data system ini tidak dapat di hapus";
							throw new Exception(Yii::t("dialog","data with id $id is locked.."));
						}else{
							if (!$studentItem->delete()){
								$this->result["actionMessage"] = "Data ini tidak dapat di hapus";
								throw new Exception(Yii::t("dialog",json_encode($studentItem->getErrors())));
							}	
						}
					}else{
						$studentItem->item_desc= $itemDesc;
						$studentItem->item_price= $itemPrice;
						$studentItem->pendapatan_acc_id = $pendapatan_acc_id;
						$studentItem->disc_acc_id = $disc_acc_id;
						$studentItem->is_active = $is_active;
						if(!$studentItem->save()) {
							$this->result["actionMessage"] = "Gagal menyimpan data transaksi";
							throw new Exception(Yii::t("dialog",json_encode($studentItem->getErrors())));
						}
					}
				}
				$this->result["actionStatus"] = 0;
			}catch (Exception $e){
				//array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());
				$this->result["actionStatus"] = 1;				 
				$this->result["actionError"] = $e->getMessage();
			}
			$this->returnJSON($this->result);
			
		}
		
		protected function beforeRender($view){
			if ($this->subMenu != ""){
				$this->title = $this->mainMenu . " - " .$this->subMenu;
			}
			return true;
		}
		
	}
?>